package com.example.g7640.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.g7640.builder.ResultBuilder;
import com.example.g7640.dto.common.PageResult;
import com.example.g7640.dto.common.Result;

import java.util.List;

public class PageUtil {

    public static IPage pageToMybatisPage(com.example.g7640.dto.common.Page  page){
        if(page == null){
            return null;
        }
        IPage mybatisPage = new Page(page.getPageNo(),page.getPageSize());
        return mybatisPage;
    }


    public static <T> PageResult<List<T>> mybatisPageToPage(IPage<T> mybatisPage){
        if(mybatisPage == null){
            return null;
        }
        long total = mybatisPage.getTotal();
        PageResult<List<T>> page = new PageResult<>();
        page.setCode(ResultBuilder.CODE_SUCCESS);
        page.setMsg(ResultBuilder.MES_SUCCESS);
        page.setTotalNum((int)total);
        page.setPageNo((int)mybatisPage.getCurrent());
        page.setPageSize((int) mybatisPage.getSize());
        page.setData(mybatisPage.getRecords());
        return page;
    }


    public static <T> PageResult<List<T>> mybatisPageToPage(IPage<T> mybatisPage,Class<T> clazz){
        if(mybatisPage == null){
            return null;
        }
        long total = mybatisPage.getTotal();
        PageResult<List<T>> page = new PageResult<>();
        page.setCode(ResultBuilder.CODE_SUCCESS);
        page.setMsg(ResultBuilder.MES_SUCCESS);
        page.setTotalNum((int)total);
        page.setPageNo((int)mybatisPage.getCurrent());
        page.setPageSize((int) mybatisPage.getSize());
        page.setData(BaseConvertor.convertList(mybatisPage.getRecords(),clazz));
        return page;
    }
}
